package com.test.algorithm;

import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.Arrays;
import java.util.Collection;

//分表范围查询
public class MyRangeTableShardingAlgorithm implements RangeShardingAlgorithm<Long> {
    /**
     * select * from course where cid between 1 and 100;
     *
     * @param availableTargetNames 真实表集合 [course_1，course_2]
     * @param shardingValue  逻辑表course和主键，以及范围值
     * @return 真实表集合
     */
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Long> shardingValue) {
        //逻辑表名:course
        String logicTableName = shardingValue.getLogicTableName();
        //主键列名
        String columnName = shardingValue.getColumnName();
        //主键范围值的最大值
        Long upperVal = shardingValue.getValueRange().upperEndpoint();//100
        //主键范围值的最小值1
        Long lowerVal = shardingValue.getValueRange().lowerEndpoint();//1

        //范围查询返回所有真实表
        return Arrays.asList(logicTableName + "_1", logicTableName + "_2");
    }
}
